package com.robotic.sensor.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.robotic.common.utils.DateUtils;
import com.robotic.common.utils.StringUtils;
import com.robotic.common.exception.ServiceException;
import com.robotic.sensor.mapper.RegionStandardMapper;
import com.robotic.sensor.domain.RegionStandard;
import com.robotic.sensor.service.IRegionStandardService;

/**
 * 区域标准配置Service业务层处理
 * 
 * @author robotic
 * @date 2024-01-20
 */
@Service
public class RegionStandardServiceImpl implements IRegionStandardService
{
    @Autowired
    private RegionStandardMapper regionStandardMapper;

    /**
     * 查询区域标准配置
     * 
     * @param id 区域标准配置主键
     * @return 区域标准配置
     */
    @Override
    public RegionStandard selectRegionStandardById(Long id)
    {
        return regionStandardMapper.selectRegionStandardById(id);
    }

    /**
     * 根据区域代码和传感器类型查询区域标准配置
     * 
     * @param regionCode 区域代码
     * @param sensorType 传感器类型
     * @return 区域标准配置
     */
    @Override
    public RegionStandard selectRegionStandardByCodeAndType(String regionCode, String sensorType)
    {
        return regionStandardMapper.selectRegionStandardByCodeAndType(regionCode, sensorType);
    }

    /**
     * 查询区域标准配置列表
     * 
     * @param regionStandard 区域标准配置
     * @return 区域标准配置
     */
    @Override
    public List<RegionStandard> selectRegionStandardList(RegionStandard regionStandard)
    {
        return regionStandardMapper.selectRegionStandardList(regionStandard);
    }

    /**
     * 查询启用状态的区域标准配置列表
     * 
     * @param regionCode 区域代码（可选）
     * @return 区域标准配置集合
     */
    @Override
    public List<RegionStandard> selectActiveRegionStandardList(String regionCode)
    {
        return regionStandardMapper.selectActiveRegionStandardList(regionCode);
    }

    /**
     * 根据区域代码查询所有传感器类型的标准配置
     * 
     * @param regionCode 区域代码
     * @return 区域标准配置集合
     */
    @Override
    public List<RegionStandard> selectRegionStandardByCode(String regionCode)
    {
        return regionStandardMapper.selectRegionStandardByCode(regionCode);
    }

    /**
     * 根据传感器类型查询所有区域的标准配置
     * 
     * @param sensorType 传感器类型
     * @return 区域标准配置集合
     */
    @Override
    public List<RegionStandard> selectRegionStandardByType(String sensorType)
    {
        return regionStandardMapper.selectRegionStandardByType(sensorType);
    }

    /**
     * 新增区域标准配置
     * 
     * @param regionStandard 区域标准配置
     * @return 结果
     */
    @Override
    public int insertRegionStandard(RegionStandard regionStandard)
    {
        regionStandard.setCreateTime(DateUtils.getNowDate());
        return regionStandardMapper.insertRegionStandard(regionStandard);
    }

    /**
     * 修改区域标准配置
     * 
     * @param regionStandard 区域标准配置
     * @return 结果
     */
    @Override
    public int updateRegionStandard(RegionStandard regionStandard)
    {
        regionStandard.setUpdateTime(DateUtils.getNowDate());
        return regionStandardMapper.updateRegionStandard(regionStandard);
    }

    /**
     * 批量删除区域标准配置
     * 
     * @param ids 需要删除的区域标准配置主键
     * @return 结果
     */
    @Override
    public int deleteRegionStandardByIds(Long[] ids)
    {
        return regionStandardMapper.deleteRegionStandardByIds(ids);
    }

    /**
     * 删除区域标准配置信息
     * 
     * @param id 区域标准配置主键
     * @return 结果
     */
    @Override
    public int deleteRegionStandardById(Long id)
    {
        return regionStandardMapper.deleteRegionStandardById(id);
    }

    /**
     * 批量插入区域标准配置
     * 
     * @param regionStandardList 区域标准配置列表
     * @return 结果
     */
    @Override
    public int batchInsertRegionStandard(List<RegionStandard> regionStandardList)
    {
        for (RegionStandard regionStandard : regionStandardList)
        {
            regionStandard.setCreateTime(DateUtils.getNowDate());
        }
        return regionStandardMapper.batchInsertRegionStandard(regionStandardList);
    }

    /**
     * 更新区域标准配置状态
     * 
     * @param id 区域标准配置主键
     * @param status 状态（0-禁用，1-启用）
     * @return 结果
     */
    @Override
    public int updateRegionStandardStatus(Long id, String status)
    {
        return regionStandardMapper.updateRegionStandardStatus(id, status);
    }

    /**
     * 校验区域代码和传感器类型组合是否唯一
     * 
     * @param regionStandard 区域标准配置信息
     * @return 结果
     */
    @Override
    public String checkRegionStandardUnique(RegionStandard regionStandard)
    {
        Long regionStandardId = StringUtils.isNull(regionStandard.getId()) ? -1L : regionStandard.getId();
        RegionStandard info = regionStandardMapper.selectRegionStandardByCodeAndType(regionStandard.getRegionCode(), regionStandard.getSensorType());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != regionStandardId.longValue())
        {
            return "区域代码和传感器类型组合已存在";
        }
        return "0";
    }

    /**
     * 导入区域标准配置数据
     * 
     * @param regionStandardList 区域标准配置数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importRegionStandard(List<RegionStandard> regionStandardList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(regionStandardList) || regionStandardList.size() == 0)
        {
            throw new ServiceException("导入区域标准配置数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RegionStandard regionStandard : regionStandardList)
        {
            try
            {
                // 验证是否存在这个区域标准配置
                RegionStandard r = regionStandardMapper.selectRegionStandardByCodeAndType(regionStandard.getRegionCode(), regionStandard.getSensorType());
                if (StringUtils.isNull(r))
                {
                    regionStandard.setCreateBy(operName);
                    regionStandard.setCreateTime(DateUtils.getNowDate());
                    this.insertRegionStandard(regionStandard);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、区域 " + regionStandard.getRegionCode() + " 传感器 " + regionStandard.getSensorType() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    regionStandard.setId(r.getId());
                    regionStandard.setUpdateBy(operName);
                    regionStandard.setUpdateTime(DateUtils.getNowDate());
                    this.updateRegionStandard(regionStandard);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、区域 " + regionStandard.getRegionCode() + " 传感器 " + regionStandard.getSensorType() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、区域 " + regionStandard.getRegionCode() + " 传感器 " + regionStandard.getSensorType() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、区域 " + regionStandard.getRegionCode() + " 传感器 " + regionStandard.getSensorType() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}